#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include "libradarssd.h"

#define SIGNAL_PER_FILE     2048
#define SIGNAL_READ         2048
#define TEST_TIME           10000
#define MAX_FILE_NAME       (8*21845-1)     //修改前面的倍数即可
#define MAX_INDEX           (8*44738560-SIGNAL_PER_FILE)
//#define ROOT_PATH           "/home/gary/Code/dataset/radar_fs_test/%d"
#define ROOT_PATH           "/mnt/f2fs/%d"

unsigned long elapsed(struct timeval* begin, struct timeval* end){
    long seconds = end->tv_sec - begin->tv_sec;
    long microseconds = end->tv_usec - begin->tv_usec;
    return seconds*1e6 + microseconds;
}

//读取文件的第一个TOA值
u64 get_file_head(unsigned int file_number){
    FILE* fp = NULL;
    char path[60] = {0};
    sprintf(path, ROOT_PATH, file_number);
    fp = fopen(path, "r");

    u64 head_value = 0;
    fread(&head_value, sizeof(u64), 1, fp);
    fclose(fp);

    return head_value;
}

int main(){
    struct timeval begin, end;
    gettimeofday(&begin, 0);

    srand((unsigned)time(NULL));
    unsigned long read_signal = 0;
    for(int i=0;i<TEST_TIME;i++){
        ftype toa = rand() % MAX_INDEX + 1;
        //printf("toa:%lu\n",toa);

        //找到查询的toa所在的文件
        unsigned int head=0, tail=MAX_FILE_NAME;
        unsigned int start_file=0;
        //查询首尾两个文件查看是否在范围内
        while(1){
            if(head >= tail){
                start_file = head;
                break;
            }
            //读取首尾两个文件的起始toa
            u64 head_next_start = get_file_head(head+1);
            u64 tail_start = get_file_head(tail);
            // printf("head:%lu tail:%lu\n",head_next_start,tail_start);
            // return 0;
            if(toa < head_next_start){
                start_file = head;
                break;
            }
            if(toa >= tail_start){
                start_file = tail;
                break;
            }

            unsigned int middle = (head+tail)/2;
            u64 middle_start = get_file_head(middle);
            if(toa > middle_start){
                head = middle;
            }
            else if(toa < middle_start){
                tail = middle;
            }
            else{
                start_file = middle;
                break;
            }
        }
        
        //从该文件开始读取
        unsigned remain_read = SIGNAL_READ;
        u64 result[SIGNAL_READ] = {0};
        while(remain_read > 0){
            //打开文件
            FILE* fp = NULL;
            char path[60] = {0};
            sprintf(path, ROOT_PATH, start_file);
            fp = fopen(path, "r");

            u64 value[FEATURE_COUNT] = {0}; //用于接收文件数据
            while(1){
                unsigned readed = fread(&value, sizeof(u64), FEATURE_COUNT, fp);
                if(readed == 0 || remain_read <= 0) //如果文件读完了退出这个文件
                    break;
                if(value[0] < toa) //如果读到的数据小于toa的值则继续找下一组数据
                    continue;
                else{
                    result[SIGNAL_READ-remain_read] = value[0];
                    remain_read--;
                }
            }
            fclose(fp);
            read_signal += SIGNAL_READ;
            start_file++;
        }
        // for(int i=0;i<SIGNAL_READ;i++)
        //     printf("%lu\n",result[i]);
    }

    gettimeofday(&end, 0);

    printf("total elapsed %lu us(%f s)\n",elapsed(&begin, &end),elapsed(&begin, &end)/1000000.0);

    float MB = read_signal*FEATURE_SIZE / (float)(1024*1024);
    float bandwith = MB/((double)elapsed(&begin, &end)/1000000);
    double average_latency = (double)elapsed(&begin, &end)/read_signal;
    printf("read %lu signals in %.2f MB\nBandwith:%.2f MB/s\nAverage latency:%.4lf us/signal\n",read_signal,MB,bandwith,average_latency);

    return 0;
}

